import org.springframework.security.providers.UsernamePasswordAuthenticationToken as AuthToken
import org.springframework.security.context.SecurityContextHolder as SCH

class TeacherController {
    def authenticateService
    def daoAuthenticationProvider

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.order = "desc"
        params.sort = "createTime"
        [teacherInstanceList: Teacher.list(params), teacherInstanceTotal: Teacher.count()]
    }

    def create = {
        def teacherInstance = new Teacher()
        teacherInstance.properties = params
        return [teacherInstance: teacherInstance]
    }

    def save = {
        def teacherInstance = new Teacher(params)

        if (params.passwd != params.repasswd) {
			teacherInstance.passwd = ''
			flash.message = '两次密码输入不一致'
			render(view: "create", model: [teacherInstance: teacherInstance])
			return
		}
        def config = authenticateService.securityConfig
		def teacherRole = config.security.teacherRole

		def role = Role.findByAuthority(teacherRole)
        teacherInstance.passwd = authenticateService.encodePassword(teacherInstance.passwd)
        if (teacherInstance.save(flush: true)) {
            role.addToPeople(teacherInstance) 
            teacherInstance.save(flush: true)

            def auth = new AuthToken(teacherInstance.username, params.passwd)
			def authtoken = daoAuthenticationProvider.authenticate(auth)
			SCH.context.authentication = authtoken
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'teacher.label', default: 'Teacher'), teacherInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [teacherInstance: teacherInstance])
        }
    }

    def show = {
        def teacherInstance = Teacher.get(params.id)
        if (!teacherInstance) {
            flash.message = "留言成功"
            redirect(action: "list")
        }
        else {
            [teacherInstance: teacherInstance]
        }
    }

    def edit = {
        def teacherInstance = Teacher.get(params.id)
        if (!teacherInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [teacherInstance: teacherInstance]
        }
    }

    def update = {
        def teacherInstance = Teacher.get(params.id)
        if (teacherInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (teacherInstance.version > version) {
                    
                    teacherInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'teacher.label', default: 'Teacher')] as Object[], "Another user has updated this Teacher while you were editing")
                    render(view: "edit", model: [teacherInstance: teacherInstance])
                    return
                }
            }
            teacherInstance.properties = params
            if (!teacherInstance.hasErrors() && teacherInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'teacher.label', default: 'Teacher'), teacherInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [teacherInstance: teacherInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def teacherInstance = Teacher.get(params.id)
        if (teacherInstance) {
            try {
                teacherInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'teacher.label', default: 'Teacher'), teacherInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'teacher.label', default: 'Teacher'), teacherInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'teacher.label', default: 'Teacher'), params.id])}"
            redirect(action: "list")
        }
    }
    // 开启和关闭学生登录
    def goswitch = {
        if (params.id) {
            def teacher = Teacher.get(params.id)
            teacher.enabled = !teacher.enabled
            teacher.save(flush: true)
        }
        redirect(action: "list")
    }

        
    def search = {
        def c = Teacher.createCriteria()

        def results = c.list {
                eq('area', params.area)
                //eq('cour',params.courses)
                println it
                if(params.sex != '0'){
                    eq('sex',params.sex)
                }
                order 'id', 'desc'
        }
        def teacherInstanceList = results.findAll{
                it.courses.any{ aCourse ->
                    aCourse == params.courses
                
                }
        }

        [teacherInstanceList: teacherInstanceList,teacherInstanceListSize:teacherInstanceList.size()]
    }
}
